package com.example.lib_mvi.base

import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import com.jaeger.library.StatusBarUtil
import kotlin.math.ceil

abstract class BaseActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        injectARouter() //注入ARouter，传参接受数据
        super.onCreate(savedInstanceState)
        //设置布局
        if (-1==getLayoutId()){//没有布局id
            setContentView(getLayoutRootView())
        }else{//有布局id
            setContentView(getLayoutId())
        }
        //设置状态栏
        StatusBarUtil.setColorNoTranslucent(this,generatStatusBarColor())
        //初始化环境
        initEnv()
        //初始化view
        initView()
        //初始化数据
        initData()
        //初始化事件
        initEvent()

    }
    protected open fun initEnv(){}
    protected open fun initEvent() {}

    protected open fun initData(){}

    protected open fun initView() {}

    /**
     * 获取Content View 配合ViewBinding使用
     */
    abstract fun getLayoutRootView(): View
    /**
     * 获得布局id
     */
    protected open fun getLayoutId(layoutId:Int = -1):Int{
        return layoutId
    }
    /**
     * 设置状态栏颜色
     */
    protected open fun generatStatusBarColor(): Int{
        return Color.TRANSPARENT
    }

    protected open fun injectARouter() {

    }
    private val handler: Handler = Handler(Looper.getMainLooper())
    /**
     * 显示提示消息
     */
    fun showMsg(msg:String){
        if(Looper.getMainLooper().thread==Thread.currentThread()){ //判断当前线程是否为主线程
            Toast.makeText(this,msg, Toast.LENGTH_SHORT).show()
        }
        else{
            handler.post {
                Toast.makeText(this,msg, Toast.LENGTH_SHORT).show()
            }
        }
    }

    /**
     * 组件内跳转到指定页面
     */
    fun routeActivity(target:Class<*>){
        startActivity(Intent(this,target))
    }
    /**
     * 组件内跳转指定页面并传递参数
     */
    fun routeActivity(target: Class<*>,params:Bundle){
        val intent=Intent(this,target)
        intent.putExtra("params",params)
        startActivity(intent)
    }

    fun addFragment(fragment: Fragment, tag:String){
        supportFragmentManager.beginTransaction().add(fragment,tag).commitAllowingStateLoss()
    }
    fun hideFragment(tag:String){
        supportFragmentManager.findFragmentByTag(tag)
            ?.let {
                supportFragmentManager.beginTransaction().hide(it).commitAllowingStateLoss()
            }
    }
    fun showFragment(tag:String){
        supportFragmentManager.findFragmentByTag(tag)?.let {
            supportFragmentManager.beginTransaction().show(it).commitAllowingStateLoss()
        }
    }
    fun replaceFragment(containerId:Int,fragment:Fragment){
        supportFragmentManager.beginTransaction().replace(containerId,fragment).commitAllowingStateLoss()
    }

    fun switchFragment(fl:Int,from: Fragment?, to: Fragment) {
        if (!to.isAdded) {
            supportFragmentManager.beginTransaction().hide(from!!).add(fl, to).commit()
        } else {
            supportFragmentManager.beginTransaction().hide(from!!).show(to).commit()
        }
    }
    fun closeThis(){
        finish()
    }

    /**
     * 获取状态栏高度
     */
    private fun getStatusBarHeight(): Int {
        val identifier = resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (identifier > 0) {
            resources.getDimensionPixelSize(identifier)
        } else {
            val density = resources.displayMetrics.density
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                ceil(24 * density).toInt()
            } else {
                ceil(25 * density).toInt()
            }
        }
    }

}